package com.ilovepostcard;

import java.util.ArrayList;

import com.ilovepostcard.controller.ShareController;
import com.ilovepostcard.controller.UserController;
import com.ilovepostcard.controller.server.IServer;
import com.ilovepostcard.controller.server.ServerFactory;
import com.ilovepostcard.data.ReceiverInfo;
import com.ilovepostcard.data.SendTaskInfo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class PrintAddressActivity extends BaseActivity implements OnClickListener{
	
	ImageView mReturn;
	Button   mConfirm;
	EditText mcity;
	EditText mName;
	EditText mPhone;
	EditText mAddress;
	EditText mZipCode;
	String mProvince;
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_print_address);
		init();
	}
	
	private void init()
	{
		mConfirm = (Button)findViewById(R.id.confirm);
		mConfirm.setOnClickListener(this);
		
		mReturn = (ImageView)findViewById(R.id.title_return);
		mReturn.setOnClickListener(this);
		
		mcity  = (EditText)findViewById(R.id.address_city);
		mcity.setOnClickListener(this);
		
		mAddress  = (EditText)findViewById(R.id.address_address);
		mName  = (EditText)findViewById(R.id.address_name);
		mPhone  = (EditText)findViewById(R.id.address_phone);
		mZipCode  = (EditText)findViewById(R.id.address_zipcode);
		
	
	}
	
	 @Override
	 protected void onActivityResult(int requestCode, int resultCode, Intent data)
	 {
		 
		 if (resultCode != RESULT_OK) 
		 {
	    		Toast.makeText(this, R.string.error_selectCity, Toast.LENGTH_SHORT).show();
	    		return;
	     }
		 
		 if (requestCode == EditProfileActivity.REQUEST_CODE_CHOOSE_CITY) {
	            if (data != null) {
	            	mProvince= data.getStringExtra(PrintChooseCityActivity.KEY_PROVINCE);
	                String city = data.getStringExtra(PrintChooseCityActivity.KEY_CITY);
	                mcity.setText(city);
	            }
	        }
	 }
	 
	 
	 
	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.title_return)
		{
			finish();
		}
		else if(v.getId() == R.id.confirm)
		{

			String name = mName.getEditableText().toString();
			String address = mAddress.getEditableText().toString();
			if(!checkValid(name)) return;
			if(!checkValid(address)) return;
			
			SendTaskInfo taskInfo = ShareController.getInstance().getCurrentSendTask();
			if(taskInfo.recevierList == null)
				taskInfo.recevierList = new ArrayList<ReceiverInfo>();
			ArrayList<ReceiverInfo> list = taskInfo.recevierList;
			ReceiverInfo info = new ReceiverInfo();
			if(mcity.getEditableText() != null)
				info.recevicerCity = mcity.getEditableText().toString();
			
			if(!checkValid(info.recevicerCity )) return;
			if(mAddress.getEditableText() != null)
				info.recevierAddress = mAddress.getEditableText().toString();
			
			if(!checkValid(info.recevierAddress)) return;
			if(mName.getEditableText() != null)
				info.recevierName = mName.getEditableText().toString();
			
			if(! checkValid(info.recevierName)) return;
			if(mPhone.getEditableText() != null)
				info.recevierPhone = mPhone.getEditableText().toString();
			
			if(!checkValid(info.recevierPhone)) return;
			if(mZipCode.getEditableText() != null)
				info.recevierPostNumber = mZipCode.getEditableText().toString();

			if(info.recevierPostNumber.length() != 6)
			{
				Toast.makeText(this, R.string.print_address_tip, Toast.LENGTH_SHORT).show();
				return;
			}
			list.add(info);
			
			
			IServer server = ServerFactory.getServer();
			server.newAddress(UserController.getLoginUser(this).UserID, info.recevierName, info.recevierPhone, mProvince, info.recevicerCity, info.recevierAddress, info.recevierPostNumber);
			Intent nextPage = new Intent(PrintAddressActivity.this,PrintChooseAddressActivity.class);
			PrintAddressActivity.this.startActivity(nextPage);
		}
		else if(v.getId() == R.id.address_city)
		{
			 Intent intent = new Intent(this, PrintChooseCityActivity.class);
	         startActivityForResult(intent, EditProfileActivity.REQUEST_CODE_CHOOSE_CITY);
		}
	}
	
	private boolean checkValid(String msg)
	{
		if(msg.length() == 0)
		{
			Toast.makeText(this, R.string.print_address_tip, Toast.LENGTH_SHORT).show();
			return false;
		}
		
		return true;
		
	}
	
	

}
